﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EntityModel;
using System.Data;

namespace TestingDBConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            //AddNewRecord();
        //    AddNewRecordSenzor();
            //AddNewRecordMedic();
            showCazac();
          
            Console.WriteLine("Done");
            Console.ReadLine();
        }

        private static void AddNewRecord()
        {
            // Add record to the Inventory table of the AutoLot
            // database.
            using (var context = new TeleasistentaEntities())
            {
                try
                {
                    User userPacient = new User();
                    userPacient.idUser = Guid.NewGuid();
                    userPacient.Rol = "Pacient";
                  //  userPacient.UserName = "Pacient1";
                   // userPacient.UserPassword = "parola1";

                    context.Users.AddObject(userPacient);
                    // Hard code data for a new record, for testing.
                    context.Pacients.AddObject(new Pacient()
                    {
                        idPacient = userPacient.idUser,
                        Adresa = "adresa",
                        CNP = "1234567891234",
                       // DataNasterii = DateTime.Now,
                        Email = "bla@yahoo.com",
                     //   IstoricMedical = "dfdsfdsfsdfsdfd",
                     //   ListaAlergi = "multe",
                        Nume = "Iona",
                        Prenume = "Cristina",
                        Sex = "F",
                        Telefon = "0234542",


                    });
                    context.SaveChanges();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.InnerException.Message);
                }
            }
        }

        private static void AddNewRecordSenzor()
        {
            // Add record to the Inventory table of the AutoLot
            // database.
            using (var context = new TeleasistentaEntities())
            {
                try
                {
                    var idPacient = context.Pacients.ToList()[1].idPacient;
                    /*
                    var senzor = new Senzori();
                    senzor.idPacient = idPacient;
                   senzor.idSensor=Guid.NewGuid();
           senzor.Valoare=22;
          senzor.MomentCitire=TimeSpan.FromMinutes(5);
          senzor.NrOrdine=3;
           senzor.PerioadaEsantionare="peresantionare";
           senzor.TipSenzor="temperatura";
          senzor.FactorDeScala="factorScala";
          senzor.TipFiltrare="tipfiltrare";
          senzor.NrParamFiltrare="4";
           senzor.ValoriParamFiltrare="4 3 4";
          senzor.MascaValoriIncadrare="valori incadreare";
           senzor.PragAlarmaInferior=18;
           senzor.PragAtentionareSuperior=24;
           senzor.PragAtentionareInferior=20;
           senzor.PragAlarmaSuperior=25;
           senzor.UnitateDeMasura="um";

           senzor.CurbaDeEtalonare = "curbaetalonare";

                  //  context.Senzoris.AddObject(senzor);
                    context.SaveChanges();*/
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.InnerException.Message);
                }
            }
        }

        private static void AddNewRecordMedic()
        {
            using (var context = new TeleasistentaEntities())
            {
                try
                {
                    var idPacient = context.Pacients.ToList()[2].idPacient;
              //      var user = new User() { idUser = Guid.NewGuid(), UserName = "MedicSef", UserPassword = "medic1", Rol = "Medic" };
                  //  context.Users.AddObject(user);
                    context.SaveChanges();

                    var medic = new Medic();
              //      medic.idPacient =idPacient;
                 //   medic.idMedic = user.idUser;
                    medic.Nume = "Viteazu";
                    medic.Prenume = "Mihai";
                    medic.Specializare = "Chirurgie";
                    context.Medics.AddObject(medic);
                    context.SaveChanges();

                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.InnerException.Message);
                }
            }

        }

        private static void PrintAllPacients()
        {
            using (var context = new TeleasistentaEntities())
            {
                try
                {
                    foreach (Pacient pacient in context.Pacients)
                    {
                        Console.WriteLine(pacient.Nume.ToString());
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.InnerException.Message);
                }

            }
        }

        private static void RemoveRecord()
        {
            // Find a car to delete by primary key.
            using (TeleasistentaEntities context = new TeleasistentaEntities())
            {
               
                User userTest = new User();
                //Create a user to delete
                userTest.idUser = Guid.NewGuid();
             //   userTest.UserName = "Smekeri";
             //   userTest.UserPassword = "xxx";
                userTest.Rol = "test";
                context.Users.AddObject(userTest);
                    context.SaveChanges();
                foreach (var userDel in context.Users)
	{
               //     Console.WriteLine(userDel.UserName.ToString());
                //    if(userDel.UserName.Equals(userTest.UserName))
                 //   Console.WriteLine("This user {0}, will be deleted! ",userDel.UserName.ToString());
		 
	}
                
                 // Define a key for the entity we are looking for.
                EntityKey key = new EntityKey("TeleasistentaEntities.Users","idUser",userTest.idUser);             


                // See if we have it, and delete it if we do.
                User userToDelete = (User)context.GetObjectByKey(key);
                if (userToDelete != null)
                {
                    context.DeleteObject(userToDelete);
                    context.SaveChanges();
                }


                            foreach (var userDel in context.Users)
	{
                //    Console.WriteLine(userDel.UserName.ToString());
                //    if(userDel.UserName.Equals(userTest.UserName))
                //    Console.WriteLine("This user {0}, will be deleted! ",userDel.UserName.ToString());
		 
	}
            
            }
        }

        public static void showCazac()
        {
            var context = new TeleasistentaEntities();
         //   var query = context.Medics.Where(c => c.Pacient.Medics.Any()).ToList();
          //  var x = context.Medics.Where(c => c.Pacient.Senzoris.Any());
        //    var z = x.ToList();
         //   var q = query.ToList();
            
            //var medici = context.Pacients.Where(c=> c.Medics.Any());
           
            //var query2 = context.Senzoris.Where(c => c.idPacient=context.Medics

        }
    }
    
}
